<template>
    <div>
        <h2>添加会员</h2>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>会员编号</td>
                    <td><input type="text" v-model="a.MemberCode" disabled /></td>
                </tr>
                <tr>
                    <td>电话</td>
                    <td><input type="text" v-model="a.Phone" maxlength="11" /></td>
                </tr>
                <tr>
                    <td>名字</td>
                    <td><input type="text" v-model="a.UserName" /></td>
                </tr>
                <tr>
                    <td>身份证</td>
                    <td><input type="text" v-model="a.Card" maxlength="18" /></td>
                </tr>
                <tr>
                    <td>等级</td>
                    <td><select v-model="a.LevelId">
                            <option value="">请选择</option>
                            <option :value="item.Id" v-for="item in xlk.list">{{ item.Name }}</option>
                        </select></td>
                </tr>
                <tr>
                    <td>生日</td>
                    <td><input type="date" v-model="a.Birthday" /></td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td><input type="radio" name="sex" :value="1" v-model="a.Sex" />男
                        <input type="radio" name="sex" :value="0" v-model="a.Sex" />女
                    </td>
                </tr>
                <tr>
                    <td>积分</td>
                    <td><input type="text" v-model="a.Score" /></td>
                </tr>
                <tr>
                    <td>余额</td>
                    <td><input type="text" v-model="a.Money" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" class="btn btn-primary" @click="add" /></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router';
import moment from 'moment';
const router = useRouter();

var num=parseInt((Math.random()*1000).toString())+100;

let xlk:any=reactive({
  list:[]
})
onMounted(() => {
   
    getList();
})

let a:any=reactive({
    MemberCode: moment(new Date()).format('YYYY-MM-DD') + num,
    Phone: '',
    UserName: '',
    Card: '',
    LevelId: '',
    Birthday: '',
    Sex: 1,
    Score: '',
    Money: ''
})

const add=()=>{
    if (a.Phone == '') {
        alert('手机号不能为空');
        return;
    }
    let reg1 = /^1[3-9]\d{9}$/;
    if (!reg1.test(a.Phone)) {
        alert('手机号不合法');
        return;
    }
    if (a.UserName == '') {
        alert('姓名不能为空');
        return;
    }
    let reg2 = /^\d{17}[0-9xX]$/;
    if (!reg2.test(a.Card)) {
        alert('身份证不合法');
        return;
    }
    if (a.Card == '') {
        alert('身份证号不能为空');
        return;
    }
    if (a.LevelId == '') {
        alert('等级不能为空');
        return;
    }
    if (a.Birthday == '') {
        alert('生日不能为空');
        return;
    }
    if (a.Score == '') {
        alert('积分不能为空');
        return;
    }
    if (isNaN(parseInt(a.Score)) == true) {
        alert("积分必须为数字");
        return;
    }
    if (a.Money == '') {
        alert('余额不能为空');
        return;
    }
    if (isNaN(Number(a.Money))) {
        alert("余额必须为数字");
        return;
    }
    axios({
        method:'post',
        url:'/api/User/AddUser',
        data:a
    })
    .then((res)=>{
    console.log(res);
    if(res.data==-1){
      alert("身份证重复");
      return;
    }
    if(res.data>0){
      alert("添加成功");
      router.push('/user')
    }
    else{
      alert("添加失败");
      return;
    }
  })
  .catch((err)=>{
    console.log(err);
  })
}

const getList = () => {
    axios({
        method: 'get',
        url: '/api/User/ShowLevel'
    })
        .then((res) => {
            console.log(res);
            xlk.list = res.data;
        })
        .catch((err) => {
            console.log(err);
        });
}





</script>

<style scoped></style>